﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.IO;
using ModuleSettings;

namespace InnovTech.Util.Test.Settings
{
    [TestClass]
    public class TestXmlModuleSettingsProvider
    {
        [TestMethod]
        public void TestSave()
        {
            XmlModuleSettingsProvider provider = new XmlModuleSettingsProvider() { DataSource = SettingsFile_1_Path, ModuleName = SettingsModule_1_Name };
            SettingsUtil.RegisterClassProvider(First.Default, provider);
            SettingsUtil.RegisterClassProvider(Second.Default, provider);
            First.Default.Reset();
            Second.Default.Reset();
            First.Default.S1_2 = "changed";
            Second.Default.S2_1 = "changed";

            First.Default.Save();
            Assert.IsTrue(File.Exists(SettingsFile_1_Path));
            Second.Default.Save();

            var settings_1 = new First();
            var settings_2 = new Second();

            provider = new XmlModuleSettingsProvider() { DataSource = SettingsFile_1_Path, ModuleName = SettingsModule_1_Name };
            SettingsUtil.RegisterClassProvider(settings_1, provider);
            SettingsUtil.RegisterClassProvider(settings_2, provider);
            Assert.AreEqual("changed", settings_1.S1_2);
            Assert.AreEqual("changed", settings_2.S2_1);
        }

        [TestMethod]
        public void TestUpgrade()
        {
            XmlModuleSettingsProvider provider = new XmlModuleSettingsProvider() { DataSource = SettingsFile_1_Path, ModuleName = SettingsModule_1_Name };
            SettingsUtil.RegisterClassProvider(First.Default, provider);
            SettingsUtil.RegisterClassProvider(Second.Default, provider);
            First.Default.S1_2 = "changed";
            Second.Default.S2_1 = "changed";
            First.Default.Save();
            Second.Default.Save();

            First.Default.Upgrade();
        }

        [TestMethod]
        public void TestReset()
        {
            XmlModuleSettingsProvider provider = new XmlModuleSettingsProvider() { DataSource = SettingsFile_1_Path, ModuleName = SettingsModule_1_Name };
            SettingsUtil.RegisterClassProvider(First.Default, provider);
            SettingsUtil.RegisterClassProvider(Second.Default, provider);
            First.Default.S1_1 = "changed";
            First.Default.S1_2 = "changed";
            Second.Default.S2_1 = "changed";
            First.Default.Save();
            Second.Default.Save();

            var settings_1 = new First();
            var settings_2 = new Second();

            provider = new XmlModuleSettingsProvider() { DataSource = SettingsFile_1_Path, ModuleName = SettingsModule_1_Name };
            SettingsUtil.RegisterClassProvider(settings_1, provider);
            SettingsUtil.RegisterClassProvider(settings_2, provider);
            Assert.AreEqual("changed", settings_1.S1_1);
            Assert.AreEqual("changed", settings_1.S1_2);
            Assert.AreEqual("changed", settings_2.S2_1);

            settings_1.Reset("S1_2");
            settings_1.Save();


            settings_1 = new First();
            provider = new XmlModuleSettingsProvider() { DataSource = SettingsFile_1_Path, ModuleName = SettingsModule_1_Name };
            SettingsUtil.RegisterClassProvider(settings_1, provider);
            Assert.AreEqual("2", settings_1.S1_2);
            Assert.AreEqual("changed", settings_1.S1_1);
        }

        private string SettingsFile_1_Path
        {
            get
            {
                return Directory.GetCurrentDirectory() + "\\" + "testUserSettings1.settings";
            }
        }

        private string SettingsFile_2_Path
        {
            get
            {
                return Directory.GetCurrentDirectory() + "\\" + "testUserSettings2.settings";
            }
        }

        private string SettingsModule_1_Name
        {
            get
            {
                return "TestModule_1";
            }
        }

        private string SettingsModule_2_Name
        {
            get
            {
                return "TestModule_2";
            }
        }

        private object GetValue(string dataSource, string moduleName, string name)
        {
            ModuleSettingsProviderBase provider = new XmlModuleSettingsProvider() { DataSource = dataSource, ModuleName = moduleName };
            return provider.GetType().InvokeMember("GetPropertyValue", 
                System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.InvokeMethod | System.Reflection.BindingFlags.Instance, 
                null, provider, new object[] { moduleName, name });
        }
    }
}
